/*
 * Copyright 2009 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yannickstucki.android.musicqueue.old;

import android.content.res.Resources;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.widget.AdapterView.AdapterContextMenuInfo;

import com.yannickstucki.android.musicqueue.R;
import com.yannickstucki.android.musicqueue.util.Logger;

/**
 * Creates the context menu and handles triggered calls.
 * 
 * @author Yannick Stucki (yannickstucki@gmail.com)
 * 
 */
public class ContextMenuManager {

  /**
   * Plays the selected song at once without queuing.
   */
  public static final int PLAY_NOW_ID = 0;

  /**
   * Removes the selected song from the list. Only applicable in portrait mode.
   */
  public static final int REMOVE_ID = 1;

  /**
   * Does nothing i.e. same thing as back button, but it's nice for users to be
   * able to click cancel.
   */
  public static final int CANCEL_ID = 2;

  /**
   * Does the actual work if a context item has been clicked.
   */
  private ContextMenuHandler contextMenuHandler;

  /**
   * The class needs a reference to a {@link ContextMenuHandler} to be
   * functional so it can invoke calls on the handler if a context menu item got
   * clicked.
   */
  public ContextMenuManager(ContextMenuHandler contextMenuHandler) {
    this.contextMenuHandler = contextMenuHandler;
  }

  /**
   * Creates a context menu. Always called before a context menu is shown.
   */
  public void createContextMenu(ContextMenu menu, boolean landscape,
      Resources resources) {
    menu.add(0, PLAY_NOW_ID, 0, resources.getString(R.string.play_now));
    if (!landscape) {
      menu.add(0, REMOVE_ID, 0, resources.getString(R.string.remove));
    }
    menu.add(0, CANCEL_ID, 0, resources.getString(R.string.cancel));
  }

  /**
   * Calls the appropriate method on the {@link ContextMenuHandler}. Always
   * called when a context menu item got clicked.
   */
  public boolean contextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
      case PLAY_NOW_ID:
        contextMenuHandler.playSong(info.position);
        return true;
      case REMOVE_ID:
        contextMenuHandler.removeSong(info.position);
      case CANCEL_ID:
        // Do nothing.
        return true;
      default:
        Logger.error("ERROR: No such context menu id: " + item.getItemId());
        return false;
    }
  }
}
